package com.crane.oss.web.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.Response;
import com.crane.common.core.dto.SingleResponse;
import com.crane.oss.sys.bean.request.menu.AddApiRequest;
import com.crane.oss.sys.bean.request.menu.QueryApiRequest;
import com.crane.oss.sys.bean.request.menu.UpdateApiRequest;
import com.crane.oss.sys.bean.resp.ApiBean;
import com.crane.oss.sys.service.ApiService;
import com.crane.oss.web.BaseBusinessController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author bealon
 * @email ccsu123456@qq.com
 * @date 2019年01月03日 14时07分33秒
 */

@Api(value = "ApiController", tags = {"系统管理-权限管理"})
@RestController
@RequestMapping("/sys/api")
public class ApiController extends BaseBusinessController {

    @Autowired
    private ApiService apiService;

    @ApiOperation(value = "获取接口列表信息", notes = "获取列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryApiRequest")
    @RequiresPermissions("sys:api:list")
    @PostMapping("/list")
    public MultiResponse list(@RequestBody QueryApiRequest request) {
        // 查询列表数据
        IPage<ApiBean> page = apiService.queryList(request);
        return MultiResponse.of(page);
    }

    @ApiOperation(value = "获取详细信息", notes = "获取详细信息")
    @RequiresPermissions("sys:api:info")
    @PostMapping("/info")
    public SingleResponse info(@RequestBody String data) {
        String sid = getParamter(data, "id");
        ApiBean apiBean = apiService.queryApi(Long.parseLong(sid));
        return SingleResponse.of(apiBean);
    }

    @ApiOperation(value = "新增接口", notes = "获取新增接口信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddApiRequest")
    @RequiresPermissions("sys:api:save")
    @PostMapping("/save")
    public Response save(@RequestBody @Valid AddApiRequest request) {
        apiService.save(request, getUser());
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改接口", notes = "修改接口信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateApiRequest")
    @RequiresPermissions("sys:api:update")
    @PostMapping("/update")
    public Response update(@RequestBody @Valid UpdateApiRequest request) {
        apiService.update(request, getUser());
        return Response.buildSuccess();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequiresPermissions("sys:api:delete")
    @PostMapping("/delete")
    public Response delete(@RequestBody String data) {
        String sid = getParamter(data, "id");
        apiService.delete(Long.parseLong(sid));
        return Response.buildSuccess();
    }


}
